home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / tclib.zip / QLIB.C < prev    next >
Text File  |  1987-06-03  |  4KB  |  170 lines

  1.  
  2. /* Qlib.c
  3.    Property of Borland International.
  4.    Creates files readable by the Borland linker TLINK.
  5.    Written by Roger Schlafly. (Compuserve: 76067,511)
  6.    Compile in Turbo C with
  7.      tcc -mc -w -p -O -Z -M -Id:\c -Ld:\c qlib
  8.    (assuming Turbo C is in the directory d:\c.)
  9.  
  10. This programs creates .LIB files that work with TLINK.
  11. It can be used as a primitive substitute for the librarians
  12. sold by Microsoft, Phoenix, Lattice.
  13.  
  14. The Microsoft .LIB format is not documented and is not known
  15. to Borland at this time.  The Borland linker TLINK accepts
  16. files created with the Microsoft librarian, but it is actually
  17. quite simple minded in what it accepts to be a .LIB file.
  18.  
  19. .LIB files created by QLIB work with TLINK, but not with the
  20. Microsoft linker.  QLIB does have any of the various options for
  21. modifying an existing .LIB file; it will only create one from
  22. scratch.  QLIB is, however, faster than the Microsoft librarian.
  23.  
  24. Borland may someday have a librarian which supports the Microsoft
  25. format.  If and when that happens, TLINK may no longer support
  26. the format that QLIB uses.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <io.h>
  34. #include <dos.h>
  35. #define Static static
  36.  
  37. Static FILE *rspf = 0;
  38. Static FILE *libf = 0;
  39. Static FILE *fp = 0;
  40. Static char name[81];
  41.  
  42. #define TAB \011
  43.  
  44. char delimiters[] = " \n\011,;\r";
  45. #define iswhite(x)    (strchr(delimiters,(x)) != 0)
  46.  
  47. int gettoken(void)
  48. /* return 1 on success
  49.    put token in 'name'
  50. */
  51. {    int i, c;
  52.     for (i=0; i<80; ++i)
  53.     {    c = getc(rspf);
  54.         if (c == -1) if (i == 0) return 0; else break;
  55.         if (i == 0 && iswhite(c)) { i = -1; continue;}
  56.         name[i] = (char) c;
  57.         if (iswhite(c)) break;}
  58.     name[i] = 0;
  59.     return 1;}
  60.  
  61. char *libname;
  62.  
  63. void fatal(char *s)
  64. {
  65.     puts(s);
  66.     /* don't leave partially written .LIB file on disk */
  67.     if (libf != NULL) fclose(libf);
  68.     if (libname != NULL) unlink(libname);
  69.     exit(1);
  70. }
  71.  
  72. Static int cdecl handler(void)
  73. /* control-break handler
  74. */
  75. {
  76.   fatal("Interrupted.");
  77.   return 0;}
  78.  
  79. void iofail(char *s)
  80. {
  81.     printf("%s",s);
  82.     fatal(": disk error");
  83. }
  84.  
  85. char *append(char *s, char *t)
  86. {
  87.     int n;
  88.     char *p;
  89.     n = strlen(s) + strlen(t) + 1;
  90.     p = malloc(n);
  91.     if (p == NULL) fatal("insufficient memory");
  92.     strcpy(p,s);
  93.     strcat(p,t);
  94.     return p;
  95. }
  96.  
  97. char *buf;
  98. #define MAX (unsigned) 30000
  99.  
  100. unsigned char begin[] = { 0xA4, 1, 0, 0, };
  101. unsigned char end[]   = { 0xA6, 1, 0, 0, };
  102.  
  103. int n;
  104. char *fname;
  105. int argi;
  106.  
  107. cdecl main(argc,argv)
  108. char *argv[];
  109. {
  110.     if (argc < 3)
  111.     {
  112.         puts("Borland quick librarian, version 0.20.");
  113.         puts("Usage: QLIB  libname  objfiles");
  114.         puts("where objfiles is a series of OBJ file names,");
  115.         puts("or is @name, where name is an ascii file of OBJ file names.");
  116.         puts("where objfiles is an ascii file of OBJ file names.");
  117.         puts(".LIB and .OBJ extensions added automatically.");
  118.         puts("QLIB creates a .LIB consisting of those .OBJ files.");
  119.         exit(0);
  120.     }
  121.  
  122.     ctrlbrk(handler);
  123.     libname = append(argv[1],".LIB");
  124.     libf = fopen(libname,"wb");
  125.     if (libf == NULL) printf("%s",libname), fatal(": cannot open");
  126.     printf("Creating %s\n",libname);
  127.     if (argv[2][0] == '@')
  128.     {
  129.         rspf = fopen(argv[2]+1,"r");
  130.         if (rspf == NULL)
  131.             printf("%s",argv[2]), fatal(": cannot open");
  132.     }
  133.     argi = rspf == NULL ? 2 : 0;
  134.  
  135.     buf = malloc(MAX);
  136.     if (buf == NULL) fatal("insufficient memory");
  137.  
  138.     fwrite(begin,1,4,libf);
  139.  
  140. while (1)
  141. {
  142.     if (argi)
  143.     {
  144.         if (argi >= argc) break;
  145.         strcpy(name,argv[argi++]);
  146.     }
  147.     else if (!gettoken()) break;
  148.  
  149.     fname = append(name,".OBJ");
  150.     fp = fopen(fname,"rb");
  151.     if (fp == NULL) iofail(fname);
  152. #ifdef    Portable
  153.     if (fseek(fp,0L,2)) iofail(fname);
  154.     n = ftell(fp);
  155.     if (fseek(fp,0L,0)) iofail(fname);
  156.     n = fread(buf,1,MAX,fp);
  157. #else    /* TURBO C */
  158.     /* manual says n = 0 if at EOF, but don't believe it. */
  159.     n = _read(fileno(fp),buf,MAX);
  160. #endif
  161.     if (n == MAX) fatal("OBJ file too large");
  162.     if (fclose(fp)) iofail(fname);
  163.     printf("Adding %s, %d bytes.\n",fname,n);
  164.     fwrite(buf,1,n,libf);
  165. }
  166.     fwrite(end,1,4,libf);
  167.     if (fclose(libf)) iofail(libname);
  168.     exit(0);
  169. }
  170.